package com.book.analyer;

import com.book.HibernateManager;
import com.book.PageListAnalyze;
import com.book.bean.PageListItem;
import sun.security.krb5.Config;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class DefaultPageListAnalyer  implements PageListAnalyze {
     private String rootURL=null;
     private Integer pageNum=0;
     private Integer totalPageNum=0;
     private HibernateManager hibernateManager;
     private static final ThreadPoolExecutor excutor=new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
             new ArrayBlockingQueue<Runnable>(5));


    @Override
    public void init(com.book.Config config, HibernateManager hibernateManager) {

    }

    /**
     * 根据第一个页面获取总页数 跟链接
     * @param url
     */
    public int   findPage(URL url){

      return 0;
    }

    /**
     * 爬取数据写入数据库
     * @return
     */
    public int  analyerPageList(){
        excutor.execute(new Runnable() {
            @Override
            public void run() {
                  String  url="http://www.java1234.com/a/javabook/javabase/list_65_3.html";
                try {
                    List<PageListItem>  itemList=parserItem(new URL(url));
                    hibernateManager.saveBatch(itemList);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }


            }
        });

     return 0;
    }
   private List<PageListItem>  parserItem(URL url){
        List<PageListItem> items=new ArrayList<>();


        return items;
   }


}
